package github.pop.ares.entity;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.IPacket;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * step 1 create a custom entity
 */
public class FlyingSwordEntity extends Entity {
    private Logger logger = LogManager.getLogger();
    // sync data muse be static
    // u can change generic type custom you define type
    private static final DataParameter<Integer> COUNTER = EntityDataManager.defineId(FlyingSwordEntity.class, DataSerializers.INT);


    public FlyingSwordEntity(EntityType<?> p_i48580_1_, World p_i48580_2_) {
        super(p_i48580_1_, p_i48580_2_);
    }

    @Override
    protected void defineSynchedData() {
        // register type
        this.entityData.define(COUNTER,0);
    }

    @Override
    protected void readAdditionalSaveData(CompoundNBT compound) {
        //read data
        this.entityData.set(COUNTER,compound.getInt("counter"));
    }

    @Override
    protected void addAdditionalSaveData(CompoundNBT compound) {
        // save data
        compound.putInt("counter",
                this.entityData.get(COUNTER));
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        // entity create by server side
        // after notice client
        // so it not use super()
        // must be use forge provide method come to send message
        return NetworkHooks.getEntitySpawningPacket(this);
    }

    @Override
    public void tick() {
        // entity must be implement tick()
        // it is importantest method
        boolean isClientSide = level.isClientSide;

        if(!isClientSide){
            logger.info(this.entityData.get(COUNTER));
        }

        if(isClientSide){
            logger.info(this.entityData.get(COUNTER));
            // set and increment plus one
            this.entityData.set(COUNTER, this.entityData.get(COUNTER) + 1);
        }
        super.tick();
    }
}
